#include "NetworkSession.h"

namespace mp_net
{
	NetworkSession::NetworkSession()
		: Updateable(), RecursiveLockingMutex()
	{ 
		//m_ioService = NULL; 
		m_socket = NULL; 
		m_port = NET_SESSION_DEF_PORT;
		m_operatingThread = NULL;
		m_stopRequested = false;
	}

	NetworkSession::~NetworkSession()
	{
		//Close the sessions operating thread
		if ( m_operatingThread != NULL )
		{
			//Join network thread
			m_stopRequested = true;
			m_operatingThread->join();

			delete m_operatingThread;
		}
		//finally remove the io service

		m_ioService.stop();
		//delete m_ioService;
		
		Unlock();
	}


	void NetworkSession::StartOperatingThread()
	{
		if ( m_operatingThread == NULL )
		{
			//Setup process thread
			m_stopRequested = false;
			m_operatingThread = new boost::thread 
				(boost::bind(&NetworkSession::NetThreadProc, this));
		}
	}

	void NetworkSession::StopOperatingThread()
	{
		if ( m_operatingThread != NULL )
		{
			m_stopRequested = true;
			m_operatingThread->join();
			m_operatingThread = NULL;
			m_stopRequested = false;
		}
	}

}